package com.github.xuemingyu.springboot.acticitycloud.driver.message.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.xuemingyu.springboot.acticitycloud.driver.message.Message;
import org.springframework.context.annotation.Configuration;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class MessageImpl implements Message {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        int c = 1;
        for (int i = 0; i < 201; i++) {
            list.add(c);
            c++;
        }

        int y = list.size() % 200;
        int s = list.size() / 200;
        if (y > 0) {
            s = s + 1;
        }
        Map<String,List<Integer>> map = new HashMap<>();
        for (int i = 0;i<s;i++){
            if (i == s-1){
                map.put(i+"",list.subList(i*200,list.size()));
            }else {
                map.put(i+"",list.subList(i * 200, (i+1)*200));
            }
        }
        System.out.println(map);
    }

    @Override
    public void sendMessage(List<String> mobile, String msg) throws IOException {
        JSONArray tel = new JSONArray();
        mobile.forEach(q -> {
            JSONObject telElement = new JSONObject();
            telElement.put("mobile", q);
            tel.add(telElement);
        });
        JSONObject data = new JSONObject();
        data.put("tel", tel);
        data.put("Appid", "10034"); //申请的appid
        data.put("Appkey", "666666"); //应用的密码key
        data.put("nation", "86"); //发送的短信国家码
        data.put("Tmpl", "0");//模板id（由信息中心申请，为0则自定义内容发送，不需要模板，param只用put一个即可）
        JSONObject param1 = new JSONObject();
        param1.put("param", msg);  //变量一
        JSONArray content = new JSONArray();
        content.add(param1);
        System.out.println(data);
        //post发送到服务器端
        String wholeurl = "http://10.113.4.40:8080/SMS/AppuserAction.action"; //服务器短信发送接口
        URL object = new URL(wholeurl);
        HttpURLConnection conn;
        conn = (HttpURLConnection) object.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Accept", "*/*");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
        wr.write(data.toString());
        wr.flush();

        //接收服务器发来的回复
        StringBuilder sb = new StringBuilder();
        int httpRspCode = conn.getResponseCode();

        if (httpRspCode == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            System.out.println(sb + "123456");    //将服务器回复输出
        }

    }
}
